﻿<?php
session_start();
ini_set('max_execution_TIME', 600);
include("dbconfig.php");
$userid="admin";
$sign="admin";

$examp = $_REQUEST["q"]; 

$page = $_REQUEST['page']; 
$limit = $_REQUEST['rows']; 
$sidx = $_REQUEST['sidx']; 
$sord = $_REQUEST['sord']; 
if(!$sidx) $sidx =1;
$style=-1;
//搜索与初始化时候传出的参数

if(isset($_GET["style"]))
	{
	$style = $_GET['style'];
	}
else{
	$style= 0;
	}
	//echo $style;

if(isset($_GET["rev_mask"]))
	{
	$rev_mask = $_GET['rev_mask'];
	}
else{
	$rev_mask= '';
	}
	
if($style==0)
{
$where = " WHERE 1=1   ";
if($rev_mask!='')
    $where.= " AND email like '%$rev_mask%'";

$db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");
$SQL ="select * from white_email ".$where;
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();
$count = mysql_num_rows($result);

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL ="select * from white_email ".$where."ORDER BY $sidx $sord LIMIT $start , $limit";

$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
	 $responce->rows[$i]['ID']=$row[id];
    $responce->rows[$i]['cell']=array($row[id],$row[email]);
    $i++;
} 

echo json_encode($responce);
mysql_close($db);

}

else if($style==1){
$where = " WHERE 1=1   ";
$db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");

$sql="select count(*) as count from white_email where email = '$rev_mask' AND userid like '$userid' AND sign like '$sign' ";
$result =mysql_query ($sql);
if (!$result) echo "SQL错误0：".mysql_error();
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count==0)
{
$sql="Insert into white_email(email,userid,sign) values('$rev_mask','$userid','$sign')";
$result =mysql_query ($sql);
if (!$result) echo "SQL错误1：".mysql_error();
}
else
{
	 echo "已经存在";
}

$SQL ="select * from white_email ".$where;
$result = mysql_query($SQL);
if (!$result) echo "SQL错误：".mysql_error();
$count = mysql_num_rows($result);

if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL ="select * from white_email ".$where."  ORDER BY $sidx $sord LIMIT $start , $limit";
//echo $SQL;
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;

while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
	 $responce->rows[$i]['ID']=$row[id];
    $responce->rows[$i]['cell']=array($row[id],$row[email]);
    $i++;
} 

echo json_encode($responce);
mysql_close($db);
}

else if($style==2)
{
   $db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
   or die("Connection Error: " . mysql_error());

   mysql_select_db($database) or die("Error conecting to db.");
   	switch ($_REQUEST['oper']) {
		case 'del':
			delete();
			break;
		case 'edit':
			update($_POST);
			break;
		default:
			break;
	}
	mysql_close($db);
} 

else if($style==3)
{
   $db = mysql_pconnect($dbhost, $dbuser, $dbpassword)
   or die("Connection Error: " . mysql_error());
   mysql_select_db($database) or die("Error conecting to db.");
   upload();
	mysql_close($db);

?>
<?php $url = "white_mail_manager_ft.php";?>
   <html>
   <head>
   <meta http-equiv="refresh" content="3; url=<?php echo $url; ?>">
   </head>
   <body>
3秒种后跳转
</body>
</html>	
<?php }?>




<?php
function delete(){
		 $id=$_REQUEST['id'];
		 $var=explode(",",$id);
		 $len=count($var);
		 for ($i=0;$i<$len;$i++)
		 {
		 $sql="Delete from white_email where id='$var[$i]'";
		 $result =mysql_query ($sql); 
		 if (!$result) echo "SQL错误：".mysql_error();
		 }			 
	    }

function update($data) {
		  $id=$_REQUEST['ID'];
		  $email=$_REQUEST['EMAIL'];
		 $sql="Update white_email set email='$email' where id='$id'";
		 $result =mysql_query ($sql);
		 if (!$result) echo "SQL错误：".mysql_error();

	 }

function upload($data){
$userid="admin";
$sign="admin";
if ((($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "text/txt"))
&& ($_FILES["file"]["size"] < 2000000))
{
	if ($_FILES["file"]["error"] > 0)
	{
		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	}
	else
	{

			move_uploaded_file($_FILES["file"]["tmp_name"],
			"upload/" . $_FILES["file"]["name"]);
			$countre=0;
			$target_path = 'upload/';
			$target_path = $target_path . $_FILES["file"]["name"];
			$hander= fopen($target_path,"r");
			while(!feof($hander))
			{
				$tmpmail=fgets($hander);
				$count++;

				if($tmpmail!="")
				{
					$sql="select count(*) as count from white_email where email = '$tmpmail' AND userid like '$userid' AND sign like '$sign' ";
					$result= mysql_query($sql);
					if (!$result) echo "SQL错误：".mysql_error();
					$row = mysql_fetch_array($result,MYSQL_ASSOC);
					$count = $row['count'];
					if($count==0)
					{
						$sql="Insert into white_email(email,userid,sign) values('$tmpmail','$userid','$sign')";
						$result =mysql_query ($sql);
						if (!$result) echo "SQL错误1：".mysql_error();
					}
					else
					{
						$countre++;
					}
				}

			}

			fclose($hander);
			echo "重复条目数: " .$countre;
	}
}
else
{
	echo "Invalid file";

}



}



?>
